// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-04-24 10:28:09
// 生成路径: internal/app/business/logic/user_agents.go
// 生成人：周志雄
// desc:代理商用户
// company:云南奇讯科技有限公司
// ==========================================================================

package logic

import (
	"context"
	"github.com/gogf/gf/v2/crypto/gmd5"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/api/v1/business"
	"github.com/tiger1103/gfast/v3/internal/app/business/dao"
	"github.com/tiger1103/gfast/v3/internal/app/business/model"
	"github.com/tiger1103/gfast/v3/internal/app/business/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/business/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterUserAgents(New())
}

func New() *sUserAgents {
	return &sUserAgents{}
}

type sUserAgents struct{}

func (s *sUserAgents) List(ctx context.Context, req *business.UserAgentsSearchReq) (listRes *business.UserAgentsSearchRes, err error) {
	listRes = new(business.UserAgentsSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.UserAgents.Ctx(ctx).WithAll()
		if req.Username != "" {
			m = m.Where(dao.UserAgents.Columns().Username+" like ?", "%"+req.Username+"%")
		}
		if req.Name != "" {
			m = m.Where(dao.UserAgents.Columns().Name+" like ?", "%"+req.Name+"%")
		}
		if req.Phone != "" {
			m = m.Where(dao.UserAgents.Columns().Phone+" = ?", req.Phone)
		}
		if req.Status != "" {
			m = m.Where(dao.UserAgents.Columns().Status+" = ?", req.Status)
		}
		if req.AgentId != "" {
			m = m.Where(dao.UserAgents.Columns().AgentId+" = ?", gconv.Int(req.AgentId))
		}

		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		var res []*model.UserAgentsInfoResVo
		err = m.Fields("user_agents.*, agents.name AS agentName").LeftJoin("agents on agents.id = user_agents.agent_id").
			Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.UserAgentsListResVo, len(res))
		for k, v := range res {
			listRes.List[k] = &model.UserAgentsListResVo{
				Id:        v.Id,
				Username:  v.Username,
				Name:      v.Name,
				Phone:     v.Phone,
				Status:    v.Status,
				AgentId:   v.AgentId,
				AgentName: v.AgentName,
				CreatedAt: v.CreatedAt,
			}
		}
		listRes.Total = len(listRes.List)
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
	})
	return
}

func (s *sUserAgents) GetById(ctx context.Context, id int) (res *model.UserAgentsInfoResVo, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.UserAgents.Ctx(ctx).Fields("user_agents.*, agents.name AS agentName").
			InnerJoin("agents on agents.id=user_agents.agent_id").WithAll().Where(dao.UserAgents.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sUserAgents) Add(ctx context.Context, req *business.UserAgentsAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		encryptedPassword, _ := gmd5.Encrypt(req.Password)
		_, err = dao.UserAgents.Ctx(ctx).Insert(do.UserAgents{
			Username: req.Username,
			Password: encryptedPassword,
			Name:     req.Name,
			Phone:    req.Phone,
			Status:   1,
			AgentId:  req.AgentId,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sUserAgents) Edit(ctx context.Context, req *business.UserAgentsEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.UserAgents.Ctx(ctx).WherePri(req.Id).Update(do.UserAgents{
			Username: req.Username,
			Password: req.Password,
			Name:     req.Name,
			Phone:    req.Phone,
			Status:   req.Status,
			AgentId:  req.AgentId,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sUserAgents) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.UserAgents.Ctx(ctx).Delete(dao.UserAgents.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
